//
//  LXChooseBtnTableViewCell.m
//  ZuLinProject
//
//  Created by bancheng on 2021/12/21.
//  Copyright © 2021 LX_App. All rights reserved.
//

#import "LXChooseBtnTableViewCell.h"
@interface LXChooseBtnTableViewCell()

@property(nonatomic)UIButton *selBtn;

@end

@implementation LXChooseBtnTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier WithData:(NSArray *)data{
    
    self = [super initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.backgroundColor = White_Color;
        
        self.titleLab = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, 145, kComHeight)];
        self.titleLab.numberOfLines = 2;
        self.titleLab.font = Fond16;
        self.titleLab.textColor = ColorWith333333;
        [self.contentView addSubview:self.titleLab];
       
        
        CGFloat backgroundView_X = MaxX(self.titleLab);
        CGFloat backgroundView_W = kScreenWidth - MaxX(self.titleLab);

//        CGFloat backgroundView_H = [LXChooseBtnTableViewCell height:data];
        CGFloat backgroundView_H = 0;
        
        
        
        UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(backgroundView_X, 0, backgroundView_W, backgroundView_H)];
//        backgroundView.backgroundColor = UIColor.redColor;
        [self.contentView addSubview:backgroundView];
        

        // 间距
        CGFloat margin = 10;
        CGFloat btnW = backgroundView_W / 3 - margin;
        CGFloat btnH = 30;
        
       
        
        for (int i = 0; i < data.count; i++) {
           
            
            NSInteger row = i/3;
            NSInteger col = i%3;
            
            
            CGFloat btnX =  col  * (btnW + margin) ;
            CGFloat btnY = margin + row * (btnH + margin);
            
            NSDictionary *dic = data[i];
            NSString *title = dic[@"title"];
            
            
            UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(btnX, btnY, btnW, btnH)];
            [btn setTitle:title forState:UIControlStateNormal];
            
            // 把数据源id当做tag,以便回调时方便实用
            btn.tag = [dic[@"id"] integerValue];
            [btn setTitleColor:ColorWithAAAAAA forState:UIControlStateNormal];
            [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
            btn.titleLabel.font = Fond14;
            [btn setBackgroundColor:ColorWithF5F5F5];
            btn.layer.cornerRadius = 3;
            btn.layer.masksToBounds = YES;
            
            [btn addTarget:self action:@selector(clcikBtn:) forControlEvents:UIControlEventTouchUpInside];
            [backgroundView addSubview:btn];
            
            if ([dic[@"sel"] boolValue]) {
                btn.selected = YES;
                [btn setBackgroundColor:mainColor];
                self.selBtn = btn;
            }
            
            backgroundView_H = btn.Y_height + margin;
            
        }
        backgroundView.height = backgroundView_H;
        
       
        
        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, backgroundView_H-0.5, kScreenWidth, 0.5)];
        line.backgroundColor = ColorWithF5F5F5;
        [self.contentView addSubview:line];
        
        
    }
    return self;
}

-(void)clcikBtn:(UIButton *)sender{
    
    if (self.canEdit==NO)return;;
    
    self.selBtn.selected = !self.selBtn.isSelected;
   [self.selBtn setBackgroundColor:ColorWithF5F5F5];
    
    
    sender.selected = !sender.isSelected;
    self.selBtn = sender;
    [self.selBtn setBackgroundColor:mainColor];

    if (self.clickBtnWithBlock) {
        self.clickBtnWithBlock(sender.titleLabel.text, sender.tag);
        return;
    }
    
    if (self.clickBtnBlock) {
        self.clickBtnBlock(sender.tag);
    }
    
    
    
}

+(CGFloat)height:(NSArray *)data{
    CGFloat height = data.count/3 * kComHeight;
    if (data.count % 3!=0) {
        height = (data.count/3 + 1) * kComHeight;
    }
    CGFloat count = data.count/3.0;
    if (count > 1) {
        return height - (5 * ceil(count));
    }
    return height;
}


@end
